
// ImagesLoader.java
// Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th

/* The Imagesfile and images are stored in "Images/"
   (the IMAGE_DIR constant).

   ImagesFile Formats:

    o <fnm>                     // a single image file

    n <fnm*.ext> <number>       // a series of numbered image files, whose
                                // filenames use the numbers 0 - <number>-1

    s <fnm> <number>            // a strip file (fnm) containing a single row
                                // of <number> images

    g <name> <fnm> [ <fnm> ]*   // a group of files with different names;
                                // they are accessible via  
                                // <name> and position _or_ <fnm> prefix

    and blank lines and comment lines.

    The numbered image files (n) can be accessed by the <fnm> prefix
    and <number>. 

    The strip file images can be accessed by the <fnm>
    prefix and their position inside the file (which is 
    assumed to hold a single row of images).

    The images in group files can be accessed by the 'g' <name> and the
    <fnm> prefix of the particular file, or its position in the group.


    The images are stored as BufferedImage objects, so they will be 
    manipulated as 'managed' images by the JVM (when possible).
*/


import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;     // for ImageIcon
public class ImageLoader {

	public BufferedImage image;
	private GraphicsConfiguration gc;

	void LoadImage(String filename)
	/* Load the image from <fnm>, returning it as a BufferedImage.
      Uses ImageIcon.
	 */
	{ 
		BufferedImage TempImage;
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		gc = ge.getDefaultScreenDevice().getDefaultConfiguration();

		try{
			TempImage = ImageIO.read(getClass().getResource(filename) );
			if (TempImage == null)
				System.out.println("Failed go get image");

			int transparency = TempImage.getColorModel().getTransparency();

			image =  gc.createCompatibleImage(
								   TempImage.getWidth(), TempImage.getHeight(),
								   transparency );
			// create a graphics context
			Graphics2D g2d = image.createGraphics();
			// g2d.setComposite(AlphaComposite.Src);

			// reportTransparency(IMAGE_DIR + fnm, transparency);

			// copy image
			g2d.drawImage(TempImage,0,0,null);
			g2d.dispose();
		} catch(IOException e) {
			System.out.println("Load Image error for ");
		}
	}  // end of loadImage() using ImageIcon


}
